home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 3_14.lha / 3_14 / 3_14e.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  1KB  |  50 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / Interpret the function whose definition
  6. / is stored in the string funcdefn, using the
  7. / arguments newargs.
  8. ifdef STRSTREAM
  9. include <strstream.h>
  10. endif
  11. ouble interpfunc(double *newargs, char *funcdefn)
  12.  
  13. /cerr << "interpfunc(" << funcdefn << ")\n";        /* DELETE */
  14.    // save old values
  15.    istream savecin = cin;
  16.    token_value savecurr_tok = curr_tok;
  17.    double *saveargs = curfuncargs;
  18.  
  19.    // set new values
  20. ifdef STRSTREAM
  21.    strstreambuf str(strlen(funcdefn), funcdefn);
  22.    istream ncin(&str);
  23.    cin = ncin;
  24. else
  25.    istream *ncin = new istream(strlen(funcdefn), funcdefn);
  26.    cin = *ncin;
  27. endif
  28.    curfuncargs = newargs;
  29.    
  30.    // interpret the string
  31.    double val = 0;
  32.    while (cin)
  33. {
  34. get_token();
  35. if (curr_tok == END) break;
  36. if (curr_tok == PRINT) continue;
  37. val = expr();
  38. }
  39. /cerr << "interpfunc() returns " << val << "\n";        /* DELETE */
  40.    
  41.    // restore the old values
  42. ifndef STRSTREAM
  43.    delete ncin;
  44. endif
  45.    curfuncargs = saveargs;
  46.    curr_tok = savecurr_tok;
  47.    cin = savecin;
  48.    return val;
  49.  
  50.